package com.sky.mapper;

import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import com.sky.vo.OrderVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;

//订单表
@Mapper
public interface OrdersMapper {

    //插入订单数据
    void insert(Orders orders);

    /**
     * 根据订单号查询订单
     * @param orderNumber
     */
    @Select("select * from orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);

    /**

     * 用于替换微信支付更新数据库状态的问题

     * @param orderStatus

     * @param orderPaidStatus

     */

    @Update("update orders set status = #{orderStatus},pay_status = #{orderPaidStatus} ,checkout_time = #{check_out_time} where number = #{orderNumber}")

    void updateStatus(Integer orderStatus, Integer orderPaidStatus, LocalDateTime check_out_time, String orderNumber);
    //分页查找订单
    List<OrderVO> getOrders(OrdersPageQueryDTO ordersPageQueryDTO);
    //根据订单id查询订单
    OrderVO getOrderDetailsById(Long id);
    //根据订单id查询订单信息
    @Select("select * from orders where id = #{id}")
    Orders getById(Long id);
    //统计个状态订单数量
    List<Map<Integer, Object>> statisticOrder();
    //查找指定状态和时间下的订单信息
    @Select("select * from orders where status = #{status} and order_time < #{time}")
    List<Orders> getByStatusTime(Integer status, LocalDateTime time);
    //批量更新订单
    void updateBatch(List<Orders> orders);

    //查询指定日期区间的营业额
    @Select("select DATE_FORMAT(order_time,'%Y-%m-%d') date1,SUM(amount) money from orders where" +
            "    status=5 and DATE_FORMAT(order_time,'%Y-%m-%d') between #{begin} and #{end} " +
            "group by  DATE_FORMAT(order_time,'%Y-%m-%d')")
    List<Map> getTurnOver(LocalDate begin, LocalDate end);
    //查询指定定日期内各订单状态的数量、总金额
    @Select("select status,count(*) mount,sum(amount) money from orders where order_time " +
            "between #{begin} and #{end} group by status")
    List<Map> getOrdersCount(LocalDateTime begin,LocalDateTime end);
    //查询指定日期内的每天订单情况
    @Select("select DATE_FORMAT(order_time,'%Y-%m-%d') date,status,sum(amount) money,count(*) amount" +
            " from orders where DATE_FORMAT(order_time,'%Y-%m-%d') between #{begin} and #{end}" +
            " group by DATE_FORMAT(order_time,'%Y-%m-%d'),status")
    List<Map> getOrderCountEveryDay(LocalDate begin,LocalDate end);
}
